package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceClassService;
import com.eastjet.maintenance.domain.DeviceClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *班组管理
 * */
@CrossOrigin
@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private DeviceClassService deviceClassService;

    @RequestMapping("/list")
    public Page<DeviceClass> getClassList(@RequestParam(defaultValue = "1") int page,
                                                @RequestParam(defaultValue = "2") int size,
                                                @RequestParam(defaultValue ="") String condition
    )
    {
        System.out.println("condition:" + condition);
        return deviceClassService.getClassList(page,size,condition);
    }

    @RequestMapping("/add")
    public String add(@RequestBody Map<String, Object> map)
    {
        String TableName;
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.get("TableName") == null || map.isEmpty()){
            //返回错误信息
            return "TableName is null or empty";
        }else {
            TableName = (String) map.get("TableName");
            DeviceClass deviceClass = new DeviceClass();
            deviceClass.setClassName(map.get("className").toString());
            deviceClass.setChargePerson(Integer.valueOf(map.get("chargePerson").toString()));
            deviceClass.setClassType(map.get("classType").toString());
            deviceClass.setClassNotes(map.get("classNotes").toString());
            return deviceClassService.addDeviceClass(deviceClass,TableName);
        }
    }

    @RequestMapping("/update")
    public String update(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceClass deviceClass = new DeviceClass();
        deviceClass.setClassId(Integer.valueOf(map.get("classId").toString()));
        deviceClass.setClassName(map.get("className").toString());
        deviceClass.setChargePerson(Integer.valueOf(map.get("chargePerson").toString()));
        deviceClass.setClassType(map.get("classType").toString());
        deviceClass.setClassNotes(map.get("classNotes").toString());
        return deviceClassService.updateDeviceClass(deviceClass);
    }

    @RequestMapping("/delete")
    public String delete(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceClass deviceClass = new DeviceClass();
        deviceClass.setClassId(Integer.valueOf(map.get("classId").toString()));
        deviceClass.setIsDelete(Integer.valueOf(map.get("isDelete").toString()));
        return deviceClassService.deleteDeviceClass(deviceClass);
    }

    @RequestMapping("/updateState")
    public String updateState(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceClass deviceClass = new DeviceClass();
        deviceClass.setClassId(Integer.valueOf(map.get("classId").toString()));
        deviceClass.setClassTypes(Integer.valueOf(map.get("classTypes").toString()));
        return deviceClassService.updateDeviceClassState(deviceClass);
    }
}
